package com.google.code.mamule.process;

import java.util.LinkedList;
import java.util.List;

import com.google.code.mamule.mail.Mail;
import com.google.code.mamule.mail.Mailable;
import com.google.code.mamule.print.PrintDocument;

public class MailRendererImpl implements MailRenderer{

	@Override
	public Iterable<PrintDocument> apply(Mail mail) {
		String senderText="";
		List<PrintDocument> iterables=new LinkedList<PrintDocument>();
		
		//no salutation for sender block
		if(mail.getSender().getCorporation()!=null){
			senderText+= mail.getSender().getCorporation().getGivenName() +  mail.getSender().getCorporation().getSurName() + "\n";
		}
		
		senderText+=mail.getSender().getName().getSurName() + " " +  mail.getSender().getName().getGivenName().toUpperCase() + "\n";
		senderText+=mail.getSender().getAddress().getResidence() + "\n";
		senderText+=mail.getSender().getAddress().getPostcode() + " " +mail.getSender().getAddress().getLocality() + "\n";
		senderText+=mail.getSender().getAddress().getCountry();
		
		
		//TODO: variable expansion!!
		for(Mailable tmp:mail.getReceiver()){
			//What does getSalutation() contain exactly? just something linke Sehr geehrter Herr...? or just like Herr Ing. ... 
	
			String receiverText="To ";
			if(tmp.getCorporation()!=null){
				receiverText+=tmp.getCorporation().getSalutation() + "\n" + tmp.getCorporation().getGivenName()+ tmp.getCorporation().getSurName();
			}
			
			receiverText+=tmp.getName().getSalutation()+ "\n"+ tmp.getName().getSurName() + " " + tmp.getName().getGivenName().toUpperCase() + "\n";
			receiverText+=tmp.getAddress().getResidence() + "\n";
			receiverText+=tmp.getAddress().getPostcode() + " " + tmp.getAddress().getLocality() + "\n";
			receiverText+=tmp.getAddress().getCountry();		
			
			iterables.add(new PrintDocument(senderText,receiverText,mail.getText()));
		}
				
	   		
		return iterables;
	}

}
